<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>canvas时钟</title>
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    html,
    body{
        width: 100%;
        height: 100%;
        background-color: #000;
    }
    canvas{
        position: relative;
        top: calc(50% - 250px);
        left: calc(50% - 250px);;
    }
    </style>
</head>
<body>
    <canvas width="500" height="500"></canvas>
    <script>
        var myCanvas = document.getElementsByTagName('canvas')[0];
        var ctx = myCanvas.getContext('2d');

        ctx.strokeStyle = '#0ff';
        ctx.lineWidth = 7;
        ctx.shadowBlur = 15;
        ctx.shadowColor = '#0ff';

        function renderTime() {
            var date = new Date();
            var today = date.toDateString();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            var ms = date.getMilliseconds();
            var rms = s + ms / 1000;
            var grandient = ctx.createRadialGradient(250,250,5,250,250,300);
            grandient.addColorStop(0,"#03303a");
            grandient.addColorStop(1,'#000');
            ctx.fillStyle = grandient;
            ctx.fillRect(0,0,500,500);
            ctx.beginPath();
            ctx.arc(250,250,200,deg(270),deg(h * 30 - 90));
            ctx.stroke();
            ctx.beginPath();
            ctx.arc(250,250,170,deg(270),deg(m * 6 - 90));
            ctx.stroke();
            ctx.beginPath();
            ctx.arc(250,250,140,deg(270),deg(rms * 6 - 90));
            ctx.stroke(); 
            ctx.font = '25px Helvetica';
            ctx.fillStyle = '#0ff';
            ctx.fillText(today,175,250);
            ctx.fillText(format(h)+":"+format(m)+":"+format(s)+":"+(ms+"").slice(0,2),175,280);
        }
        function deg(d){
            var f = Math.PI / 180; 
            return d * f;
        }
        function format(n){
            return ("0" + n).slice(-2); 
        }

        setInterval(renderTime,60)
    </script>

    
</body>
</html>